package upms.api.fallback;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import upms.api.feign.RemoteUserService;
import upms.api.vo.UsersVo;

import java.util.Collections;
import java.util.Set;

/**
 * https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/#spring-cloud-feign-circuitbreaker-fallback
 */
@Slf4j
@Component
public class RemoteUserFallback implements FallbackFactory<RemoteUserService> {
    @Override
    public RemoteUserService create(Throwable cause) {
        log.error("服务调用失败:{}", cause.getMessage());

        return new RemoteUserService() {
            @Override
            public UsersVo info(String username) {
                return new UsersVo().setUsername("e").setPassword("{noop}e");
            }

            @Override
            public Set<String> role(String username) {
                return Collections.singleton("anonymous");
            }
        };
    }
}